package com.wyzz.hr.bo.impl;

import java.math.BigDecimal;
import java.util.*;
import org.apache.axis.utils.StringUtils;

import com.wyzz.hr.pojo.Attendshift;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;

public class ExaminDateUtil {

	private ExaminDateUtil() {
	}

	public static ExaminDateUtil getInstance() {
		if (dateUtil == null)
			dateUtil = new ExaminDateUtil();
		return dateUtil;
	}

	public static int isTimeInShift(Date checkDT, Date empshiftDate,
			String attsSession) {
		Date shiftDT[] = getSplitDateByAttendShift(empshiftDate, attsSession);
		int segments = shiftDT.length / 2;
		for (int i = 0; i < segments; i++) {
			int j = 2 * i;
			long diff1 = DateUtil.dateDiff(shiftDT[j], checkDT, 13);
			long diff2 = DateUtil.dateDiff(checkDT, shiftDT[j + 1], 13);
			if (diff1 > 0L && diff2 > 0L)
				return 2;
			if (diff1 == 0L && diff2 == 0L)
				return 4;
			if (diff1 == 0L)
				return 1;
			if (diff2 == 0L)
				return 3;
		}

		return 0;
	}

	public static int minutesInShift(Date checkDateTimeStart,
			Date checkDateTimeEnd, Date empshiftDate, String attsSession) {
		Date shiftDateTime[] = getSplitDateByAttendShift(empshiftDate,
				attsSession);
		int segments = shiftDateTime.length / 2;
		int totalMinutes = 0;
		for (int i = 0; i < segments; i++) {
			int j = 2 * i;
			if (checkDateTimeStart.after(shiftDateTime[j]))
				shiftDateTime[j] = checkDateTimeStart;
			if (checkDateTimeEnd.before(shiftDateTime[j + 1]))
				shiftDateTime[j + 1] = checkDateTimeEnd;
			if (shiftDateTime[j].before(shiftDateTime[j + 1]))
				totalMinutes += DateUtil.dateDiff(shiftDateTime[j],
						shiftDateTime[j + 1], 12);
		}

		return totalMinutes;
	}

	public static Date[] getShiftArr(Date empshiftDate, String attsSession) {
		Date shiftDateTime[] = getSplitDateByAttendShift(empshiftDate,
				attsSession);
		Date resultArr[] = new Date[2];
		resultArr[0] = shiftDateTime[0];
		resultArr[1] = shiftDateTime[shiftDateTime.length - 1];
		return resultArr;
	}

	public static double getDefaultHoursPerDay() {
		String leaveMode = (String) DatabaseSysConfigManager.getInstance()
				.getProperties().get("sys.examin.leave.mode");
		if (leaveMode == null || leaveMode.trim().equals(""))
			leaveMode = "8";
		double hour = 8D;
		try {
			hour = Double.parseDouble(leaveMode);
		} catch (Exception e) {
			hour = 8D;
		}
		return hour;
	}

	public static int getOtMatch() {
		String otMatch = (String) DatabaseSysConfigManager.getInstance()
				.getProperties().get("sys.examin.ot.match");
		if (StringUtils.isEmpty(otMatch))
			return 0;
		else
			return Integer.parseInt(otMatch);
	}

	public static Date[] getDateArray(String yearmonth, String monthSum) {
		yearmonth = yearmonth.replaceAll("-", "");
		int year = Integer.parseInt(yearmonth.substring(0, 4));
		int month = Integer.parseInt(yearmonth.substring(4));
		if (monthSum == null || monthSum == "")
			monthSum = "1-1";
		String monthSumArray[] = monthSum.split("-");
		Integer beginMonth = Integer
				.valueOf(Integer.parseInt(monthSumArray[0]));
		Integer beginDay = Integer.valueOf(Integer.parseInt(monthSumArray[1]));
		Calendar cal = new GregorianCalendar();
		if (beginMonth.intValue() == 1) {
			cal.set(year, month - 1, beginDay.intValue(), 0, 0, 0);
			cal.set(14, 0);
		} else {
			cal.set(year, month - 2, beginDay.intValue(), 0, 0, 0);
			cal.set(14, 0);
		}
		Date beginDate = cal.getTime();
		Date endDate = DateUtil.dateTimeAdd(cal.getTime(), 1, 2);
		endDate = DateUtil.dateAdd(endDate, -1);
		Date startEndDates[] = { beginDate, endDate };
		return startEndDates;
	}

	public static Date[] getDateArray(Date date, String monthSum) {
		int year = DateUtil.getYear(date);
		int month = DateUtil.getMonth(date);
		int day = DateUtil.getDay(date);
		Date beginDate = DateUtil.getYearMonthFirstDay(year, month);
		if (monthSum != null) {
			String monthSumArray[] = monthSum.split("-");
			if ("0".equals(monthSumArray[0])) {
				Integer dayOffset = Integer.valueOf(Integer
						.parseInt(monthSumArray[1]) - 1);
				beginDate = DateUtil.dateAdd(beginDate, dayOffset.intValue());
				if (day < dayOffset.intValue())
					beginDate = DateUtil.dateTimeAdd(beginDate, -1, 2);
			}
		}
		Date endDate = DateUtil.dateAdd(DateUtil.dateTimeAdd(beginDate, 1, 2),
				-1);
		Date startEndDates[] = { beginDate, endDate };
		return startEndDates;
	}

	public static Date[] getSplitDateByAttendShift(Date empShiftDate, String str) {
		String timeRange[] = str.split(":|-|,");
		Date shiftDateTime[] = new Date[timeRange.length / 2];
		for (int i = 0; i < timeRange.length / 2; i++) {
			int hh = Integer.parseInt(timeRange[2 * i]);
			int mi = Integer.parseInt(timeRange[2 * i + 1]);
			shiftDateTime[i] = DateUtil.parseDateTimeToD(empShiftDate, hh, mi,
					0);
		}

		return shiftDateTime;
	}

	public static Double getShiftHours(String str, Integer type) {
		if (StringUtils.isEmpty(str))
			return Double.valueOf(0.0D);
		Date dateRange[] = getSplitDateByAttendShift(new Date(), str);
		double result = 0.0D;
		if (type.intValue() == 0)
			result = DateUtil.dateDiff(dateRange[0], dateRange[1], 10);
		else
			result = DateUtil.dateDiff(dateRange[2], dateRange[3], 10);
		return Double.valueOf(result);
	}

	public static BigDecimal getExaminDatePeriod(BigDecimal minutes,
			String config) {
		if (minutes == null || minutes.compareTo(new BigDecimal(0)) == 0)
			return new BigDecimal(0);
		String arrs[] = config.split(";");
		for (int i = arrs.length - 1; i >= 0; i--) {
			String period[] = arrs[i].split(",");
			if (period.length != 2)
				continue;
			BigDecimal from = new BigDecimal(period[0]);
			BigDecimal to = new BigDecimal(period[1]);
			if (minutes.compareTo(from) > 0 && minutes.compareTo(to) <= 0)
				return new BigDecimal(i + 1);
			if (minutes.compareTo(to) > 0)
				return new BigDecimal(i + 1);
		}

		return new BigDecimal(0);
	}

	public static BigDecimal convertHourToDay(BigDecimal number, String config,
			Attendshift shift) {
		if (number == null || number.compareTo(new BigDecimal(0)) == 0)
			return new BigDecimal(0);
		if (shift == null)
			return new BigDecimal(0);
		String arr[] = config.split(",");
		if (arr.length == 1) {
			BigDecimal hourPerDay = new BigDecimal(config);
			return number.divide(hourPerDay, 1, 4);
		}
		BigDecimal one = new BigDecimal(arr[0]);
		BigDecimal half = new BigDecimal(arr[1]);
		BigDecimal days = number.divide(shift.getAttsWorkingHour(), 2, 4);
		BigDecimal quotient = days.divide(new BigDecimal(1), 0, 1);
		BigDecimal residual = days.subtract(quotient);
		if (residual.compareTo(one.divide(new BigDecimal(100))) > 0)
			return quotient.add(new BigDecimal(1));
		if (residual.compareTo(half.divide(new BigDecimal(100))) >= 0)
			return quotient.add(new BigDecimal(0.5D));
		else
			return quotient;
	}

	public static BigDecimal convertMinutesToHours(BigDecimal number,
			String config) {
		String configArr[] = config.split(",");
		long half = 0L;
		long one = 0L;
		one = Long.parseLong(configArr[0]);
		half = Long.parseLong(configArr[1]);
		long minus = number.longValue();
		double result = minus / 60L;
		long temp = minus % 60L;
		if (half <= temp && temp < one)
			result += 0.5D;
		else if (one <= temp)
			result++;
		return new BigDecimal(result);
	}

	public static int formatMinutes(int minute, String config) {
		String configArr[] = config.split(",");
		int half = 0;
		int one = 0;
		one = Integer.parseInt(configArr[0]);
		half = Integer.parseInt(configArr[1]);
		int temp = minute % 60;
		int result = minute - temp;
		if (half <= temp && temp < one)
			result += 30;
		else if (one <= temp)
			result += 60;
		return result;
	}

	public static Date formatDate(Date date, String config) {
		String configArr[] = config.split(",");
		long half = 0L;
		long one = 0L;
		one = Long.parseLong(configArr[0]);
		half = Long.parseLong(configArr[1]);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(13, 0);
		Integer minute = Integer.valueOf(calendar.get(12));
		long temp = minute.intValue() % 60;
		if (temp < half)
			calendar.set(12, 0);
		else if (half <= temp && temp < one) {
			calendar.set(12, 30);
		} else {
			calendar.set(12, 0);
			calendar.add(11, 1);
		}
		return calendar.getTime();
	}

	public static boolean applyLRByDay(String leaveType, Leavetype ltNo,
			Integer empShiftType) {
		Integer ltApplyByDay = null;
		if (ltNo != null)
			ltApplyByDay = ltNo.getLtApplyByDay();
		if (ltApplyByDay == null) {
			if (leaveType.equals("0"))
				return empShiftType.intValue() == 0
						|| empShiftType.intValue() == 2;
			return !leaveType.equals("1");
		}
		return ltApplyByDay.intValue() == 1;
	}

	private static ExaminDateUtil dateUtil;
}
